package com.webex.ta.hydra.util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-10-25
 * Time: 16:38:27
 */
public class ConfirmDialog extends JDialog {
    private JButton confirm = new JButton("OK");
    private JButton cancel = new JButton("Cancel");

    public ConfirmDialog(Frame owner, String title, boolean modal) {
        super(owner, title, modal);
        confirm.setPreferredSize(new Dimension(80, 21));
        cancel.setPreferredSize(new Dimension(80, 21));
        this.setLayout(new BorderLayout());
        this.getRootPane().registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOk();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.CTRL_DOWN_MASK), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        this.getRootPane().registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        add(createButtonPanel(), BorderLayout.SOUTH);
        confirm.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ConfirmDialog.this.onOk();
            }
        });
        cancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ConfirmDialog.this.onCancel();
            }
        });
    }

    private Component createButtonPanel() {
        JPanel p = new JPanel();
        p.setLayout(new FlowLayout(FlowLayout.CENTER));
        p.add(confirm);
        p.add(cancel);
        return p;
    }

    public void setContent(JComponent content) {
        add(content, BorderLayout.CENTER);
        validate();
        repaint();
    }



    protected void onOk() {
        this.setVisible(false);
    }

    protected void onCancel() {
        this.setVisible(false);
    }
}
